#include <stdio.h>
#include <stdlib.h>
#include <csp_spu.h>
#include <csp_debug.h>


#define CHANNEL_NAME 42


int global_pid; 


int Run(){

	int pid = csp_pid();

	global_pid = csp_pid();

	int n1, n2, i=0;

	void *p;


	csp_channel rChannel1;
	csp_channel rChannel2;


	rChannel1 = csp_create_channel(ANY2ANY_CHANNEL, CHANNEL_NAME, sizeof(int));
	rChannel2 = csp_create_channel(ANY2ANY_CHANNEL, CHANNEL_NAME, sizeof(int));


	csp_channel channels[] = {rChannel1, rChannel2};
	void *inputs[] = {&n1, &n2}; 

	printf("&n1=%p, &n2=%p\n", &n1, &n2);


	csp_guard_list guards = csp_create_guards(channels, 2);

	while( i < 5){
		p = malloc(5*1024);
		printf("Allocated 5KB (total of %i) at %p pid=%i.\n",(i+1)*5 , p, pid);
		
		csp_yield();

		i++;
	}

	for(i=0; i < 2; i++){
		ASSERT( guards->head[i].addr != (ls_addr_t ) inputs[i]  );
	}


	switch( csp_alt_select(guards, inputs, 2) ){
		case 0:
			/* channel value is in n1. */	
			break;
		case 1:
			/* channel value is in n2. */	
			break;
		default:
			/* skip */
			break;
	}

		csp_yield();

	for(i=0; i < 2; i++){
		ASSERT( guards->head[i].addr == (ls_addr_t ) inputs[i]  );
	}




	ASSERT( pid == csp_pid() );
	ASSERT( global_pid == csp_pid() );

	PRINTF_FOR_TESTING("%s terminating on spe #%i with pid %i\n",__FILE__ , csp_spe_id(), pid);

	PRINTF_FOR_TESTING("Nested task with pid=%i terminated with res=%i.\n", pid, res);

	printf("der!\n");

	return 0;
}
